<?php
require_once 'readmains_controller.php';
class LophpsController extends AppController {

	var $name = 'Lophps';
	var $components = array('FileUpload.FileUpload');

	function index($mon_id=null) {
		$this->Lophp->recursive = 0;
		$this->set('lophps', $this->paginate(array('mon_id'=>$mon_id)));
        $tenhp=$this->Lophp->query('select tenMon from mons where id='.$mon_id);
        $this->set(compact('mon_id','tenhp'));
	}
	
	function index1() {
		$this->Lophp->recursive = 0;
		$this->set('lophps', $this->paginate());
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid lophp', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('lophp', $this->Lophp->read(null, $id));
	}

	function add($mon_id=null) {
		if (!empty($this->data)) {
			$this->Lophp->create();
			if ($this->Lophp->save($this->data)) {
				$this->Session->setFlash(__('Lưu thành công!', true));
				$this->redirect(array('action' => 'index',$mon_id));
			} else {
				$this->Session->setFlash(__('Không thể lưu dữ liệu. Kiểm tra lại!', true));
			}
		}
		$this->Lophp->Mon->displayField='tenMon';
		$mons = $this->Lophp->Mon->find('list');
		$this->Lophp->Giangvien->displayField='tenGV';
		$giangviens = $this->Lophp->Giangvien->find('list');
		$this->set(compact('mons', 'giangviens', 'mon_id'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid lophp', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Lophp->save($this->data)) {
				$this->Session->setFlash(__('Lưu thành công!', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Không thể lưu dữ liệu. Kiểm tra lại!', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Lophp->read(null, $id);
		}
		$mons = $this->Lophp->Mon->find('list');
		$giangviens = $this->Lophp->Giangvien->find('list');
		$this->set(compact('mons', 'giangviens'));
	}

	function delete($id = null,$mon_id=null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for lophp', true));
			$this->redirect(array('action'=>'index',$mon_id));
		}
		if ($this->Lophp->delete($id)) {
			$this->Session->setFlash(__('Lớp học phần đã xóa', true));
			$this->redirect(array('action'=>'index',$mon_id));
		}
		$this->Session->setFlash(__('Lớp học phần không thể xóa', true));
		$this->redirect(array('action' => 'index',$mon_id));
	}

	function upload()
	{
		if(!empty($this->data))
		{
			
			$er=$this->FileUpload->showErrors();
			if($this->FileUpload->success||empty($er))
			{
				$err = $this->__impTodb(WWW_ROOT.DS.'files'.DS.$this->data['Lophp']['file']['name']);
				if($err)
					$this->redirect(array('action'=>'upload'),'Lỗi Import');
				else
					$this->redirect(array('action'=>'upload'),'Import Thành Công');
					
				@unlink(WWW_ROOT.DS.'files'.DS.$this->data['Lophp']['file']['name']);
			}
		 else{
                $this->Session->setFlash($er);
            }
		}
	}
	
/**
     * __impTodb($newfilename, $client_filename)
     * 
     * HÃ m nÃ y Ä‘á»�c dá»¯ liá»‡u tá»« 1 file excel (read content of excel file)
     * LÆ°u trá»¯ dá»¯ liá»‡u Ä‘á»�c Ä‘Æ°á»£c vÃ o csdl, lÆ°u theo tá»«ng dÃ²ng trÃªn excel (save to DB, save each row)
     *  
     * @param mixed $newfilename Ä‘Æ°á»�ng dáº«n Ä‘áº¿n file excel trÃªn server (the patch of excel on server)
     * @param mixed $client_filename tÃªn cá»§a file excel lÃºc táº£i lÃªn (old filename before upload)
     *          
     * @return number (==0 if succeed, otherwise there are 7 error cases)
     * 
     */ 
   function __impTodb($filename)
   {
   		$err=0;
   		set_time_limit(300);
        //doc du lieu tu tap tin tren server
        $data = new Spreadsheet_Excel_Reader($filename,true,'UTF-8');
        $dulieus = $data->dump(true,true,0);
        debug($dulieus);
   }
}
